from argparse import (
    ArgumentParser,
    RawTextHelpFormatter
)
from datetime import (
    datetime,
    timedelta
)
from rich import print
from rich.traceback import install
from cli import (
    weather_now,
    print_now,
    weather_hours,
    print_hours,
    weather_days,
    print_days,
    weather_warning,
    print_warning,
    weather_indices,
    print_indices,
    weather_history,
    print_history
)
from utils import (
    config,
    METAVAR_HELP,
    INFO_HELP,
    WEATHER_INFO_HELP,
    WEATHER_NOW_HELP,
    WEATHER_HOURS_HELP,
    WEATHER_DAYS_HELP,
    WEATHER_WARNING_HELP,
    WEATHER_INDICES_HELP,
    WEATHER_HISTORY_HELP,
    INDICES_NUM_DAYS_HELP,
    INDICES_TYPE_HELP,
    HISTORY_DATE_HELP,
    LOCATION_HELP,
    LANGUAGE_HELP,
    UNIT_HELP,
    NUM_HOURS_HELP,
    NUM_DAYS_HELP,
    print_docs
)

install()
_yesterday = datetime.now() - timedelta(days=1)
yesterday = _yesterday.strftime('%Y%m%d')

def main() -> None:
    parser = ArgumentParser(formatter_class=RawTextHelpFormatter, description=config.banner)
    # Add subparsers
    subparsers = parser.add_subparsers(dest='command', required=True, metavar=METAVAR_HELP)
    # Add info parser
    info_parser = subparsers.add_parser('info', help=INFO_HELP)
    info_parser.add_argument('info', type=str, default=None, help=WEATHER_INFO_HELP)
    # Add now parser
    now_parser = subparsers.add_parser('now', help=WEATHER_NOW_HELP)
    now_parser.add_argument('-l', '--loc', type=str, default=None, help=LOCATION_HELP)
    now_parser.add_argument('-lang', type=str, default=config.language, help=LANGUAGE_HELP)
    now_parser.add_argument('-u', '--unit', type=str, default=config.unit, help=UNIT_HELP)
    # Add hours parser
    hours_parser = subparsers.add_parser('hours', help=WEATHER_HOURS_HELP)
    hours_parser.add_argument('-l', '--loc', type=str, default=None, help=LOCATION_HELP)
    hours_parser.add_argument('-n', '--hours', type=int, default=24, help=NUM_HOURS_HELP)
    hours_parser.add_argument('-lang', type=str, default=config.language, help=LANGUAGE_HELP)
    hours_parser.add_argument('-u', '--unit', type=str, default=config.unit, help=UNIT_HELP)
    # Add days parser
    days_parser = subparsers.add_parser('days', help=WEATHER_DAYS_HELP)
    days_parser.add_argument('-l', '--loc', type=str, default=None, help=LOCATION_HELP)
    days_parser.add_argument('-n', '--days', type=int, default=3, help=NUM_DAYS_HELP)
    days_parser.add_argument('-lang', type=str, default=config.language, help=LANGUAGE_HELP)
    days_parser.add_argument('-u', '--unit', type=str, default=config.unit, help=UNIT_HELP)
    # Add warning parser
    warning_parser = subparsers.add_parser('warning', help=WEATHER_WARNING_HELP)
    warning_parser.add_argument('-l', '--loc', type=str, default=None, help=LOCATION_HELP)
    warning_parser.add_argument('-lang', type=str, default=config.language, help=LANGUAGE_HELP)
    warning_parser.add_argument('-u', '--unit', type=str, default=config.unit, help=UNIT_HELP)
    # Add indices parser
    indices_parser = subparsers.add_parser('indices', help=WEATHER_INDICES_HELP)
    indices_parser.add_argument('-l', '--loc', type=str, default=None, help=LOCATION_HELP)
    indices_parser.add_argument('-t', '--type', type=int, nargs='+', default=0, help=INDICES_TYPE_HELP)
    indices_parser.add_argument('-n', '--days', type=int, default=1, help=INDICES_NUM_DAYS_HELP)
    indices_parser.add_argument('-lang', type=str, default=config.language, help=LANGUAGE_HELP)
    indices_parser.add_argument('-u', '--unit', type=str, default=config.unit, help=UNIT_HELP)
    # Add history parser
    history_parser = subparsers.add_parser('history', help=WEATHER_HISTORY_HELP)
    history_parser.add_argument('-l', '--loc', type=str, default=None, help=LOCATION_HELP)
    history_parser.add_argument('-d', '--date', type=str, default=yesterday, help=HISTORY_DATE_HELP)
    history_parser.add_argument('-lang', type=str, default=config.language, help=LANGUAGE_HELP)
    history_parser.add_argument('-u', '--unit', type=str, default=config.unit, help=UNIT_HELP)
    # Parse arguments
    args = parser.parse_args()
    # Check language and unit
    if args.command in ['now', 'hours', 'days', 'warning', 'indices', 'history']:
        if args.lang not in config.languages:
            print(f'[bold red]Error: Language {args.lang} not supported[/]')
            print('[bold white]Supported languages[/]:',
                f'[bold green]{", ".join(config.languages)}[/]')
            return
        if args.unit not in config.units:
            print(f'[bold red]Error: Unit {args.unit} not supported[/]')
            print('[bold white]Supported units[/]:',
                f'[bold green]{", ".join(config.units)}[/]')
            return
    # Print weather
    if args.command == 'now':
        table = weather_now(args.loc, args.lang, args.unit)
        print_now(table, args.unit)
    elif args.command == 'hours':
        table = weather_hours(args.loc, args.hours, args.lang, args.unit)
        print_hours(table, args.hours, args.unit)
    elif args.command == 'days':
        table = weather_days(args.loc, args.days, args.lang, args.unit)
        print_days(table, args.days, args.unit)
    elif args.command == 'warning':
        table = weather_warning(args.loc, args.lang, args.unit)
        print_warning(table, args.unit)
    elif args.command == 'indices':
        table = weather_indices(args.loc, args.type, args.days, args.lang, args.unit)
        print_indices(table, args.days, args.unit)
    elif args.command == 'history':
        daily, hourly = weather_history(args.loc, args.date, args.lang, args.unit)
        print_history(daily, hourly, args.unit)
    elif args.command == 'info':
        print_docs(args.info)

if __name__ == '__main__':
    main()
